package com.shop2cn.iapi.sqoptrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.AddSellerMemoReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.AddSellerMemoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.ShenqiSellerProfitPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_DGSQAddSellerMemo {

	private static AddSellerMemoReq addSellerMemoReq;
	private static AddSellerMemoCall addSellerMemoCall;
	
	private static OrderShardingWapper orderShardingWrapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加买手备注");
	}

	@Before
	public void caseUp() {
		addSellerMemoReq = new AddSellerMemoReq();
		addSellerMemoCall = new AddSellerMemoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addSellerMemoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();

		long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));

		try {
			ProductManagerCallServiceV2.batchSetProductOffSale(
					ProductManagerCallServiceV2
							.getAllOnSaleProducts((int) sellerId),
					(int) sellerId);
		} catch (Exception e) {
			Logger.info("批量下架创建的商品");
		}
	}
	
	/**
	 * 
	 * @param orderId
	 * @param shopId 
	 */
	public static void verify(long orderId, long shopId) {
		
		ShenqiSellerProfitPo shenqiSellerProfitPo = orderShardingWrapper
				.selectSellerProfitbyOrderIdAndSellerId(orderId,
						addSellerMemoReq.getSellerId());
		
		Logger.verifyEquals(addSellerMemoReq.getSellerMemo(),
				shenqiSellerProfitPo.getSellerMemo(),
				"验证数据库shenqi_seller_profit.memo");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DGSQAddSellerMemo_001() {
		Logger.start(true, "创建一个订单，店主添加备注成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(productBean, autotest_goodsOwnerId);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			long sellerId = resp.getOrderList().get(0).getSellerId();
			
			addSellerMemoReq.setOrderId(orderId);
			addSellerMemoReq.setSellerId(sellerId);
			addSellerMemoReq.setSellerMemo(TestDataManager.randomStr(30) + "阿斯达");
			addSellerMemoReq.setOperatorId(1);
			
			addSellerMemoCall.setData(addSellerMemoReq);
			addSellerMemoCall.callService();

			verify(orderId, productBean.getSellerId());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DGSQAddSellerMemo_002() {
		Logger.start(true, "创建一个订单，货头添加备注成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);

			TradingServiceResp resp = SqTradingCallService.placeOrder(productBean, autotest_goodsOwnerId);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			addSellerMemoReq.setOrderId(orderId);
			addSellerMemoReq.setSellerId(autotest_goodsOwnerId);
			addSellerMemoReq.setSellerMemo(TestDataManager.randomStr(30) + "阿斯达");
			addSellerMemoReq.setOperatorId(1);
			
			addSellerMemoCall.setData(addSellerMemoReq);
			addSellerMemoCall.callService();

			verify(orderId, productBean.getSellerId());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DGSQAddSellerMemo_003() {
		Logger.start(true, "创建一个订单，中间商添加备注成功");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, false);
			
			TradingServiceResp resp = SqTradingCallService.placeOrder(productBean, autotest_goodsOwnerId);
			
			Long orderId = resp.getOrderList().get(0).getOrderId();
			
			addSellerMemoReq.setOrderId(orderId);
			addSellerMemoReq.setSellerId(productBean.getSellerIds().get(1));
			addSellerMemoReq.setSellerMemo(TestDataManager.randomStr(30) + "阿斯达");
			addSellerMemoReq.setOperatorId(1);
			
			addSellerMemoCall.setData(addSellerMemoReq);
			addSellerMemoCall.callService();
			
			verify(orderId, productBean.getSellerId());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DGSQAddSellerMemo_004() {
		Logger.start(true, "创建一个订单，客服添加备注成功");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			
			TradingServiceResp resp = SqTradingCallService.placeOrder(productBean, autotest_goodsOwnerId);
			
			Long orderId = resp.getOrderList().get(0).getOrderId();
			
			addSellerMemoReq.setOrderId(orderId);
			addSellerMemoReq.setSellerId(1234);
			addSellerMemoReq.setSellerMemo(TestDataManager.randomStr(30) + "阿斯达");
			addSellerMemoReq.setOperatorId(1);
			
			addSellerMemoCall.setData(addSellerMemoReq);
			addSellerMemoCall.callService();
			
			verify(orderId, productBean.getSellerId());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DGSQAddSellerMemo_006() {
		Logger.start(true, "一个不存在的订单，店主添加备注失败");
		try {
			
			addSellerMemoReq.setOrderId(1234);
			addSellerMemoReq.setSellerId(4321);
			addSellerMemoReq.setSellerMemo(TestDataManager.randomStr(30) + "阿斯达");
			addSellerMemoReq.setOperatorId(1);
			
			addSellerMemoCall.setData(addSellerMemoReq);
			addSellerMemoCall.callService();
			
			Logger.verifyEquals(false, addSellerMemoCall.getSuccess(), "验证success");
			Logger.verifyEquals("ErrOrderNotExists", addSellerMemoCall.getCode(), "验证errorCode");
			Logger.verifyEquals("订单号1234不存在", addSellerMemoCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DGSQAddSellerMemo_007() {
		Logger.start(true, "创建一个订单，店主添加备注，货头再次添加备注成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(productBean, autotest_goodsOwnerId);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			//第一次添加备注
			addSellerMemoReq.setOrderId(orderId);
			addSellerMemoReq.setSellerId(productBean.getSellerId());
			addSellerMemoReq.setSellerMemo(TestDataManager.randomStr(30) + "阿斯达");
			addSellerMemoReq.setOperatorId(1);
			
			addSellerMemoCall.setData(addSellerMemoReq);
			addSellerMemoCall.callService();
			
			//第二次添加备注
			addSellerMemoReq.setOrderId(orderId);
			addSellerMemoReq.setSellerId(autotest_goodsOwnerId);
			addSellerMemoReq.setSellerMemo(TestDataManager.randomStr(30) + "阿斯达");
			
			addSellerMemoCall.setData(addSellerMemoReq);
			addSellerMemoCall.callService();

			verify(orderId, productBean.getSellerId());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
